﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2013-03-21 20:07:26Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections;
using System.Collections.Generic;

namespace Outcoder.Collections
{
	public static class CollectionComparer
	{
		public static bool DictionaryContentsAreEqual<TKey, TValue>(
			IDictionary<TKey, TValue> dictionary1, IDictionary<TKey, TValue> dictionary2)
		{
			if (dictionary1 == dictionary2)
			{
				return true;
			}

			if (dictionary1 == null || dictionary2 == null)
			{
				return false;
			}

			if (dictionary1.Count != dictionary2.Count)
			{
				return false;
			}

			var valueComparer = EqualityComparer<TValue>.Default;

			foreach (var pair in dictionary1)
			{
				TValue value2;
				if (!dictionary2.TryGetValue(pair.Key, out value2))
				{
					return false;
				}

				if (!valueComparer.Equals(pair.Value, value2))
				{
					return false;
				}
			}
			return true;
		}

		public static bool DictionaryContentsAreEqualNonGeneric(IDictionary dictionary1, IDictionary dictionary2)
		{
			if (dictionary1 == dictionary2)
			{
				return true;
			}

			if (dictionary1 == null || dictionary2 == null)
			{
				return false;
			}

			if (dictionary1.Count != dictionary2.Count)
			{
				return false;
			}

			foreach (object key in dictionary1.Keys)
			{
				if (!dictionary2.Contains(key))
				{
					return false;
				}

				var value1 = dictionary1[key];
				var value2 = dictionary2[key];

				if (!Object.Equals(value1, value2))
				{
					return false;
				}
			}
			return true;
		}

		public static bool CollectionContainsSameElements<TValue>(
			ICollection<TValue> collection1, ICollection<TValue> collection2/*, Func<TValue, TValue, bool> compareFunc*/)
		{
			if (collection1 == collection2)
			{
				return true;
			}

			if (collection1 == null || collection2 == null)
			{
				return false;
			}

			if (collection1.Count != collection2.Count)
			{
				return false;
			}

			foreach (var item in collection1)
			{
				if (!collection2.Contains(item))
				{
					return false;
				}
			}

			return true;
		}

		public static bool ListsAreEqual(
			IList collection1, IList collection2/*, Func<TValue, TValue, bool> compareFunc*/)
		{
			if (collection1 == collection2)
			{
				return true;
			}

			if (collection1 == null || collection2 == null)
			{
				return false;
			}

			int listSize = collection1.Count;

			if (collection1.Count != collection2.Count)
			{
				return false;
			}

			for (int i = 0; i < listSize; i++)
			{
				if (!Object.Equals(collection1[i], collection2[i]))
				{
					return false;
				}
			}

			return true;
		}
	}
}
